using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using AutoMapper;
using SapaFinance.Domain.Model;
using SapaFinance.GenericRepository;

namespace SapaFinance.OOMap.Helpers
{
    public class BudgetItemToSelectListConvert : TypeConverter<BudgetItem, IEnumerable<SelectListItem>>
    {
        private readonly IGenericRepository<BudgetItem, Guid> _repository;

        public BudgetItemToSelectListConvert(IGenericRepository<BudgetItem,Guid> repository)
        {
            _repository = repository;
        }

        protected override IEnumerable<SelectListItem> ConvertCore(BudgetItem source)
        {
            var all = _repository.GetAll();
            if (source == null){
                return all.Select(x => new SelectListItem{
                                                             Value = x.UID.ToString(),
                                                             Text = x.Name,
                                                             Selected = false
                                                         });
            }
            return all.Select(x => new SelectListItem{
                                                         Value = x.UID.ToString(),
                                                         Text = x.Name,
                                                         Selected = source.UID == x.UID
                                                     });
        }
    }
}